<?php

namespace App\Listeners;

use App\Events\OrderPaidEvent;
use App\Services\CountProductSales\CountProductSales;
use App\Supports\Product\ProductReturnBalance;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class OrderPaidListener
{
    public function handle(OrderPaidEvent $event)
    {
        $order = $event->order;
        $user = $order->user;

	    app(CountProductSales::class)->count($order);


       /* if($user->isNotValidVip()){

            return false;
        }*/

        if ($order->isOwnOrder() || $order->sid > 0) {
            
            account_rewards($user)->plusOwnOrderRewards($order->items()->first()->sku->own_rewards, ['order_id' => $order->id]);
            if($user->parent  && $user->parent->isValidVip()){
                 account_rewards($user->parent)->plusFirstLevelOrderRewards($order->items()->first()->sku->parent_rewards,$user->name, ['order_id' => $order->id,'children_id'=>$user->id]);

            }
            if(isset($user->parent->parent)  && $user->parent->parent->isValidVip()){
                 account_rewards($user->parent->parent)->plusSecondLevelOrderRewards($order->items()->first()->sku->parent_rewards,$user->name, ['order_id' => $order->id,'children_id'=>$user->id]);

            }
        }
    }
}
